package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.mapper.MaProductMapper;
import com.zhentao.model.common.buiness.dto.AuditDto;
import com.zhentao.model.common.buiness.dto.MaProductDto;
import com.zhentao.model.common.buiness.pojo.Category;
import com.zhentao.model.common.buiness.pojo.Product;
import com.zhentao.model.common.buiness.vo.MaProductAndCategory;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.service.MaProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 王越潇
 * @version 1.0
 * @Date 2024/9/15 10:17
 * @Type
 */
@Service
public class MaProductServiceImpl extends ServiceImpl<MaProductMapper, Product> implements MaProductService {
    @Resource
    private MaProductMapper maProductMapper;

    @Override
    public ResponseResult findProduct(MaProductDto maProductDto) {
        maProductDto.checkParam();
        PageHelper.startPage(maProductDto.getPage(), maProductDto.getSize());
        List<MaProductAndCategory> all = maProductMapper.findAll(maProductDto);
        PageInfo<MaProductAndCategory> maProductAndCategoryPageInfo = new PageInfo<>(all);
        return ResponseResult.okResult(maProductAndCategoryPageInfo);
    }

    @Override
    public ResponseResult findById(Integer proid) {
        Product product = maProductMapper.selectById(proid);
        return ResponseResult.okResult(product);
    }

    @Override
    public ResponseResult auditSuccess(AuditDto auditDto) {
        Product product = maProductMapper.selectById(auditDto.getProid());
        if(product.getAudit()==3){
            product.setStatus(1);
            product.setAudit(1);
            maProductMapper.updateById(product);
        }
        return ResponseResult.okResult("人工审核通过");
    }

    @Override
    public ResponseResult auditFailure(AuditDto auditDto) {
        Product product = maProductMapper.selectById(auditDto.getProid());
        if(product.getAudit()==3){
            product.setAudit(2);
            product.setMsg(auditDto.getMsg());
            maProductMapper.updateById(product);
        }
        return ResponseResult.okResult("人工审核失败"+auditDto.getMsg());
    }

    @Override
    public ResponseResult updateStatus(Integer proid) {
        Product product = maProductMapper.selectById(proid);
        if(product.getStatus()==1){
            product.setStatus(2);
            maProductMapper.updateById(product);
            return ResponseResult.okResult("下架成功");
        }else if(product.getStatus()==2){
            product.setStatus(1);
            maProductMapper.updateById(product);
            return ResponseResult.okResult("上架成功");
        }
        return ResponseResult.okResult("修改成功");
    }
}
